home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / Applications… / All Shapes with Printing ƒ / all shapes with printing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  10.2 KB  |  336 lines  |  [TEXT/KAHL]

  1. /*
  2.     All Shapes.c
  3.     
  4.     "All Shapes" creates, manipulates, and draws all of the QuickDraw™ GX primitive shapes. As each shape is created,
  5.     it is added to "thePage". This shape represents the contents of the window. To create a shape, the user
  6.     needs to "click" within the content region of the window.
  7.     
  8.     This file contains the calls that an application needs to make the "graphics shell" work correctly.
  9.     
  10.     ©1992 - 1993  Apple Computer, Inc.
  11.     All rights reserved.
  12.     
  13.     3/22/94 - dmh - Corrected GXSetShapeAttributes usage to preserve old attributes.
  14.     8/24/94 - dmh - Universalized.
  15. */
  16.  
  17. #include <StdIO.h>
  18.  
  19. #include <events.h>
  20. #include <memory.h>
  21. #include <windows.h>
  22.  
  23. #include "font library.h"
  24. #include "graphics libraries.h"
  25. #include "graphics toolbox.h"
  26. #include "graphics debugging.h"
  27. #include "qd library.h"
  28. #include "PrintingMessages.h"
  29. #include "PrintingManager.h"
  30.  
  31. #include "graphics shell.h"
  32.  
  33.  
  34. /**  Set up the title and size of the window  **/
  35. Str255         gWindowTitle = "\p Shapes...";
  36. Rect         gWindowQDRect  = {50, 15, 475, 575};
  37.  
  38. /** 
  39.     If gDebugging = TRUE, graphics library errors and notices will be posted.  This functionality will only work with 
  40.     the "debugging" version of the QuickDraw GX init.  If this version of the init is not installed, nothing bad will happen, 
  41.     but these  functions will not work. 
  42. **/
  43. Boolean        gDebugging = true;
  44.  
  45.  
  46. /**  Set  "gGiveMeValidation" to TRUE, if you will receive run-time validation.  **/
  47. Boolean        gGiveMeValidation = true;
  48.  
  49.  
  50. /**     
  51.     gGraphicsHeapSize sets the size of the graphics heap created by calling the GXNewGraphicsClient routine
  52.     in main () within graphics shell.c.  You can determine the amount of graphics heap required by using GraphicsBug.
  53.     I'm giving it 600K, since we need abunch if we have several windows open.
  54. **/
  55. long        gGraphicsHeapSize = 600;
  56.  
  57.  
  58. /*------ DoInitialization ---------------------------------------------------------------------------------*/
  59. //
  60. //    In this function we create and gxInitialize the the private document structure for a new window.  This
  61. //    structure contains the print job and the shape which is drawn in the window.  We store this data in
  62. //    a handle and hang it off the window's refCon field for easy retrieval.  By doing this, rather than using
  63. //    globals, we can create many windows containing unique print jobs and shapes. 
  64. //
  65. OSErr DoInitialization(wind)
  66. WindowPtr wind;
  67. {
  68.     OSErr    err;
  69.     gxJob    docJob;
  70.     gxShape    docPage;
  71.     TH_Doc    windDoc;
  72.  
  73. // Create the page shape. We set the unique items attribute to make sure that each item added to the
  74. // picture has a unique reference. If this attribute was not set, we would not see all six S's rotated.
  75. // We would only see the last "S" rotated; not all 6 pieces.        
  76.  
  77.     docPage = GXNewShape(gxPictureType);
  78.     GXSetShapeAttributes(docPage, GXGetShapeAttributes(docPage)|gxUniqueItemsShape);
  79.     
  80. // Create a print job for this document.  This will be the same as the system default until the user
  81. // goes through the dialogs for Page Setup or Print…
  82.  
  83.     err = GXNewJob(&docJob);
  84.     
  85.     
  86. // If there are no errors, create a handle the size of our document structure and store the print job and page shape
  87. // in it.  Store the handle in the window's refCon field so that we can get at it.  (Note that the utility routines
  88. // "GetDocJob" and "GetDocShape" can be used to do this easily.
  89.  
  90.     if (!err)
  91.     {
  92.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  93.  
  94.         if (!windDoc)
  95.             err = MemError();
  96.         else
  97.         {
  98.             (*windDoc)->docJob = docJob;
  99.             (*windDoc)->docPage = docPage;
  100.             ((WindowPeek) wind)->refCon = (long) windDoc;
  101.         }
  102.  
  103. // Now install our application override for PrintingEvent so that we can
  104. // support the new movable-modal printing dialog boxes.
  105.  
  106.         GXInstallApplicationOverride(docJob, gxPrintingEvent, NewGXPrintingEventProc(MyPrintingEventOverride));
  107.     }
  108. }
  109.  
  110.  
  111. /*------ DoDraw ---------------------------------------------------------------------------------------*/
  112. //
  113. //    Draw the contents of the window.
  114. // 
  115. void DoDraw(wind)
  116. WindowPtr wind;
  117. {
  118.      GXDrawShape (GetDocShape(wind));
  119. }
  120.  
  121.  
  122. /*------ CreateSampleImage ----------------------------------------------------------------------------*/
  123. //
  124. //    This function creates  primitive shapes and adds them to the window's page shape.
  125. //
  126. void CreateSampleImage(WindowPtr wind)
  127. {
  128.     gxShape            thePage;
  129.     gxShape         theLine;
  130.     gxLine            lineData = {ff(25), ff(25), ff(125), ff(125)};
  131.     gxShape            theRect;
  132.     gxRectangle     rectData = {ff(25), ff(25), ff(75), ff(75)};    
  133.     gxShape         theCurve;
  134.     gxCurve         curveData = {ff(25), ff(25), ff(275), ff(75), ff(125), ff(125)};    
  135.     gxShape         thePath;
  136.     long             tripleEightData[] = {    1 /* # of contours */, 
  137.                                             6 /* # of points */, 
  138.                                             0xff000000,
  139.                                                0, 0,  // the points 
  140.                                                ff(75),  0, 
  141.                                                ff(5), ff(50),
  142.                                                ff(75),  ff(100),
  143.                                                0,  ff(100), 
  144.                                                ff(75), ff(50)};        
  145.     gxShape         theText;
  146.     gxRectangle     theTextBounds;
  147.     gxColor         textColor;
  148.     fixed            x,y;
  149.     short            loop;
  150.     gxShape         thePolygon;
  151.     long starData[] = {    1,  /**  number of contours  **/
  152.                          5 , /**  number of points  **/
  153.                         ff(60), 0, ff(90), ff(90),  ff(0), ff(30),  ff(120), ff(30), ff(0), ff(90)};   /**  the points  **/
  154.     gxShape     theBitmap;
  155.  
  156. // Retrieve the page shape so we can add to it.
  157.  
  158.     thePage = GetDocShape(wind);
  159.  
  160.  
  161. // Create a line
  162.     
  163.     theLine = GXNewLine (&lineData);
  164.  
  165.     AddToShape(thePage, theLine);
  166.     GXDisposeShape(theLine);  
  167.  
  168.  
  169. // Create a rectangle which is: red & is draw with it's frame.
  170.  
  171.     theRect = GXNewRectangle(&rectData); 
  172.      SetShapeCommonColor (theRect, red);
  173.     GXSetShapeFill (theRect, gxClosedFrameFill);
  174.     GXMoveShapeTo (theRect,  ff(150), ff(25));
  175.  
  176.     AddToShape(thePage, theRect);
  177.     GXDisposeShape(theRect);  
  178.  
  179.  
  180. // Create a curve which has: a 3.25 pen thickness
  181.     
  182.     theCurve = GXNewCurve(&curveData); 
  183.     
  184.     // The fl marco converts floating gxPoint #'s to fixed gxPoint.
  185.     GXSetShapePen(theCurve, fl(3.25));
  186.     GXMoveShapeTo (theCurve,  ff(210), ff(25));
  187.  
  188.     AddToShape(thePage, theCurve);
  189.     GXDisposeShape(theCurve);  
  190.  
  191.  
  192. // Create apath which has: a 2 pen thickness, it's gxColor is green, and it's drawn with it's frame 
  193.                                                                
  194.     thePath = GXNewPaths((gxPaths *) tripleEightData);
  195.     GXSetShapeFill (thePath, gxClosedFrameFill);
  196.      GXSetShapePen(thePath, ff(2));
  197.     SetShapeCommonColor (thePath, green);
  198.  
  199.     GXMoveShapeTo (thePath,  ff(390), ff(25));
  200.  
  201.     AddToShape(thePage, thePath);
  202.     GXDisposeShape(thePath);  
  203.  
  204.  
  205. // Create a character S which is: colored in hsv space and it is rotated 15 degrees - six times via the left bottom corner.
  206. // Create the text, set the gxFont size, and set the gxFont name
  207.  
  208.     theText = GXNewText(1,(unsigned char*)"S",  nil);
  209.     SetShapeCommonFont(theText, timesFont);
  210.     GXSetShapeTextSize(theText, ff(200));
  211.     GXMoveShapeTo (theText,  ff(25), ff(275));
  212.     GXSetShapeAttributes (theText,  gxMapTransformShape);
  213.  
  214. // Create an hsv color space and set up the initial colors
  215.  
  216.     textColor.space = gxHSVSpace;
  217.     textColor.profile = nil;
  218.     textColor.element.hsv.hue = 0x7400;
  219.     textColor.element.hsv.saturation = 0xFFFF;
  220.     textColor.element.hsv.value = 0xFFFF;
  221.  
  222. // Get the bounds of "theText" and determine the bottom left corner
  223.  
  224.     GXGetShapeBounds(theText, 0L, &theTextBounds);
  225.     x = theTextBounds.left;
  226.     y = theTextBounds.bottom;
  227.  
  228. // Rotate "theText" 15 degrees - 6 times. Add each letter to the picture.
  229.  
  230.     for (loop = 0; loop < 6; loop++) {
  231.         GXSetShapeColor(theText, &textColor);
  232.         GXRotateShape(theText, ff(15), x, y);
  233.     
  234.         AddToShape(thePage, theText);
  235.  
  236.         textColor.element.hsv.hue += 0x0940;
  237.     }
  238.     
  239.     GXDisposeShape(theText);  
  240.     
  241.                 
  242. // Create a polygon which has the following features: yellow, drawn with a pen = 3, and skew it in the vertical direction by 0.5 
  243.  
  244.     thePolygon = GXNewPolygons((gxPolygons *) starData);
  245.     GXSetShapeFill(thePolygon, gxEvenOddFill);
  246.     GXSetShapePen (thePolygon, ff(3));
  247.     SetShapeCommonColor (thePolygon, yellow);
  248.     GXMoveShapeTo (thePolygon,  ff(240), ff(110));
  249.     GXSkewShape(thePolygon, 0, fl(0.5), 0, 0);
  250.     
  251.     AddToShape(thePage, thePolygon);
  252.     GXDisposeShape(thePolygon);  
  253.  
  254.  
  255. // Retrieve a bitmap from the resource fork and skew it in the horizontal direction by 2.
  256.     
  257.     theBitmap = GetPixMapShape(128);
  258.     GXValidateShape (theBitmap);
  259.  
  260.     GXSkewShape(theBitmap, ff(2), 0, 0, 0);
  261.     GXMoveShapeTo (theBitmap,  ff(290), ff(190));
  262.     
  263.     AddToShape(thePage, theBitmap);
  264.     GXDisposeShape(theBitmap);  
  265.  
  266.  
  267. // Invalidate the window's portRect so that everything gets updated.
  268.     
  269.     SetPort(wind);
  270.     InvalRect(&wind->portRect);
  271. }
  272.  
  273.  
  274. /*------ DoCreateNew ---------------------------------------------------------------------------------*/
  275. //
  276. //    This routine is called when a window needs to be created.
  277. //
  278. OSErr DoCreateNew(void)
  279. {
  280.     OSErr        err;
  281.     WindowPtr    wind;
  282.     
  283.     /** Create a window, attach a default gxViewPort to it, create and gxInitialize our private data for it, and
  284.     /** add a sample image to its page shape. **/
  285.  
  286.     wind = NewWindow(nil, &gWindowQDRect, gWindowTitle, true, noGrowDocProc, (WindowPtr)-1L, true, 0L);
  287.     
  288.     if (!wind)
  289.         err = MemError();
  290.     else
  291.     {
  292.         SetDefaultViewPort(GXNewWindowViewPort(wind));            
  293.         err = DoInitialization(wind);
  294.     }
  295.     
  296.     CreateSampleImage(wind);
  297.     return err;
  298. }
  299.  
  300.  
  301. /*------ DoDispose -------------------------------------------------------------------------------------*/
  302. //
  303. //    This routine is called when a window needs to be disposed of.
  304. //
  305. void DoDispose(wind)
  306. WindowPtr wind;
  307. {
  308.     TH_Doc    doc;
  309.     
  310.     /**  
  311.         You should always dispose of your GX graphics objects before tossing your window. Why? It's generally good 
  312.         form and this approach guarantees that everything is disposed. If you had not disposed of everything, the
  313.         call to DisposeWindow should dispose of the objects. If you are running the debugging version of the 
  314.         SecretGraphics init with notices set, you will receive a notice that you had not disposed of everything. You
  315.         can turn notices on in this file by setting gDebugging = TRUE (above).
  316.     **/
  317.     
  318.     doc = (TH_Doc)  GetWRefCon(wind);        // Remember, this is where we stored our private data.
  319.     
  320.     GXDisposeShape(GetDocShape(wind));         // Dispose of this doc's shape.
  321.     GXDisposeJob(GetDocJob(wind));            // Dispose of this doc's print job.
  322.     DisposHandle((Handle) doc);            // Dispose of our private data.
  323.  
  324.        DisposeWindow(wind);                // Dispose of the window.
  325. }
  326.  
  327.  
  328. /*------ DoIdle ----------------------------------------------------------------------------------------*/
  329. //
  330. //    This routine is called to do things while we're idling through tthe event loop.
  331. //
  332. void DoIdle(WindowPtr wind)
  333. {
  334. }
  335.  
  336.